function received_signal = apply_channel_rsma(transmitted_signal, channel_matrix, snr_dB)
% apply_channel_rsma - 将发送信号通过MIMO信道并添加噪声
%
% 输入参数:
%   transmitted_signal - 发送信号矩阵 (num_tx_antennas x signal_length)
%   channel_matrix     - 用户的MIMO信道矩阵 (num_rx_antennas x num_tx_antennas)
%   snr_dB             - 信噪比(dB)
%
% 输出参数:
%   received_signal    - 接收信号矩阵 (num_rx_antennas x signal_length)

% 获取参数
[num_tx_antennas, signal_length] = size(transmitted_signal);
[num_rx_antennas, ~] = size(channel_matrix);

% 应用信道矩阵 - 将发送信号通过MIMO信道
received_signal_noiseless = channel_matrix * transmitted_signal;

% 计算信号功率
signal_power = mean(sum(abs(received_signal_noiseless).^2)) / num_rx_antennas;

% 将SNR从dB转换为线性比例
snr_linear = 10^(snr_dB/10);

% 计算所需的噪声功率
noise_power = signal_power / snr_linear;

% 生成复高斯白噪声
noise = sqrt(noise_power/2) * (randn(num_rx_antennas, signal_length) + ...
                             1i*randn(num_rx_antennas, signal_length));

% 添加噪声到信号
received_signal = received_signal_noiseless + noise;

% 验证接收信号的SNR
measured_signal_power = mean(sum(abs(received_signal_noiseless).^2)) / num_rx_antennas;
measured_noise_power = mean(sum(abs(noise).^2)) / num_rx_antennas;
measured_snr = 10*log10(measured_signal_power / measured_noise_power);

% 打印调试信息
% fprintf('  接收端SNR: %.2f dB (目标: %.2f dB)\n', measured_snr, snr_dB);

end 